<?php

/**
 * Qname
 *
 * @package converters
 * @author Keith Alexander
 **/
class Qname extends ConvertableObject
{
	/**
	 * separator for qnames
	 *
	 * @var string
	 **/
	public $separator = ':';
	/**
	 *  (string $qname)
	 *
	 * @return string
	 * @author Keith Alexander
	 **/
	
	/**
	 * to_term
	 *
	 * @param string | boolean $qname
	 * @param string | boolean $separator
	 * @return string
	 * @author Keith Alexander
	 **/
	public function to_term($qname=false, $separator=':')
	{
		if(!$qname)
		{
			 $qname=$this->value();
			$separator = $this->separator;
		}
		return array_pop(explode($separator,$qname));
	}

	/**
	 * to_uri(string $qname, array $namespaces, string $separator)
	 * @param string | boolean $qname
	 * @param array | boolean $namespaces
	 * @param string | boolean $separator
	 *
	 * @return object
	 * @author Keith Alexander
	 **/
	function to_uri($qname=false, $namespaces=false, $separator=':')
	{
		if(!$qname)
		{
			 $qname=$this->value();
			$separator = $this->separator;
		}

		if(!$namespaces) $namespaces = $this->namespaces();
		$parts = explode($separator,$qname);
		$prefix = array_shift($parts);
		$term = implode('',$parts);
		if(!isset($namespaces[$prefix])) return false;
		$uri_value = $namespaces[$prefix].$term;
		$uri = new URI();
		$uri->set_value($uri_value);
		return $uri;
	}
	
	/**
	 * to_eRDF_term
	 *
	 * @return string
	 * @author Keith Alexander
	 **/
	public function to_eRDF_term($qname=false, $separator=':')
	{
		if(!$qname)
		{
			 $qname=$this->value();
			$separator = $this->separator;
		}

		return str_replace($separator,'-',$qname);
	}
	
	public function is_qname($k)
	{
		if (preg_match('/^[a-zA-Z0-9]+:[a-zA-Z0-9_]+$/',$k, $matches)) return true;
		else return false;
	}
}

?>